####################################################################
## Setup
need <- c('tidyverse', 'stargazer', 'broom', 'ggthemes',
          'Hmisc', 'car', 'here', 'viridis', 'readxl', 'gtools')
have <- need %in% rownames(installed.packages())
if (any(!have)) install.packages(need[!have],repos = "http://cran.us.r-project.org")
invisible(lapply(need, library, character.only = TRUE))


# This file is to be used to generate the numbers found in Appendix Table 2. 

# WD default to detect script folder and then move as needed
rm(list = ls())
####################################################################
####################################################################

# read each sheet -- column 1
state.names <- read_excel("PSRMMatchingDetailsTable_replication_final.xlsx", sheet =1)
c1r1 <- nrow(state.names)

Federal.primary.names <- read_excel("PSRMMatchingDetailsTable_replication_final.xlsx", sheet =2)
c1r2 <- nrow(Federal.primary.names)
c1r3 <- sum(state.names$perfect_match, na.rm=TRUE)
c1r4 <- sum(state.names$imperfect_match, na.rm=TRUE)
c1r5 <- sum(state.names$`true match`, na.rm=TRUE)
c1r6 <- c1r4 - c1r5
c1r7 <- (c1r3+c1r4)/c1r1
c1r8 <- c1r3/(c1r3+c1r4)
c1r9 <- c1r6/c1r4



# read each sheet -- column 2
state.names <- read_excel("PSRMMatchingDetailsTable_replication_final.xlsx", sheet =3)
c2r1 <- nrow(state.names)

Federal.general.names <- read_excel("PSRMMatchingDetailsTable_replication_final.xlsx", sheet =4)
c2r2 <- nrow(Federal.general.names)
c2r3 <- sum(state.names$perfect_match, na.rm=TRUE)
c2r4 <- sum(state.names$imperfect_match, na.rm=TRUE)
c2r5 <- sum(state.names$`true match`, na.rm=TRUE)
c2r6 <- c2r4 - c2r5
c2r7 <- (c2r3+c2r4)/c2r1
c2r8 <- c2r3/(c2r3+c2r4)
c2r9 <- c2r6/c2r4


## These numbers match to Appendix Table 2.
#print numbers -- column 1
c1r1
c1r2
c1r3
c1r4
c1r5
c1r6
c1r7
c1r8
c1r9

#print numbers -- column 2
c2r1
c2r2
c2r3
c2r4
c2r5
c2r6
c2r7
c2r8
c2r9
